﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;


using PylonC.NETSupportLibrary;
using System.Threading;
using HalconDotNet;

using System.IO;
using System.Reflection;

namespace Test2
{
    public partial class HW缩放平移 : Form
    {
        public HW缩放平移()
        {
            InitializeComponent();
        }

        HObject xx = null;
        private HTuple width;
        private HTuple height;
        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择一张图片";
            dialog.InitialDirectory = "C:\\Users\\于立昆\\Desktop\\image2";  //初始路径
            dialog.Filter = "BMP图片|*.bmp|JPG图片|*.jpg|Gif图片|*.gif";


            if (dialog.ShowDialog() == DialogResult.OK)
            {
                // MessageBox.Show(System.IO.Path.GetFullPath(dialog.FileName).Replace("\\", "/"));
            }
            HOperatorSet.ReadImage(out xx, System.IO.Path.GetFullPath(dialog.FileName).Replace("\\", "/"));
            HOperatorSet.GetImageSize(xx, out width, out height);
            hWindowControl1.Width = width;
            hWindowControl1.Height = height;
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, -1, -1);
            HOperatorSet.DispObj(xx, hWindowControl1.HalconWindow);
        }

        #region 平移
        Point mouseDownPoint = new Point();
        bool isMove;
        private void hWindowControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;   //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
                hWindowControl1.Focus();    //鼠标滚轮事件(缩放时)需要picturebox有焦点
            }
        }
        private void hWindowControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }
        private void hWindowControl1_MouseMove(object sender, MouseEventArgs e)
        {
            hWindowControl1.Focus();    //鼠标在picturebox上时才有焦点，此时可以缩放
            if (isMove)
            {
                int x, y;           //新的hWindowControl1.Location(x,y)
                int moveX, moveY;   //X方向，Y方向移动大小。
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = hWindowControl1.Location.X + moveX;
                y = hWindowControl1.Location.Y + moveY;
                hWindowControl1.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;   //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
            }
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            panel1.Focus();  //鼠标不在picturebox上时焦点给别的控件，此时无法缩放            
            if (isMove)
            {
                int x, y;           //新的hWindowControl1.Location(x,y)
                int moveX, moveY;   //X方向，Y方向移动大小。
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = hWindowControl1.Location.X + moveX;
                y = hWindowControl1.Location.Y + moveY;
                hWindowControl1.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }
        #endregion

        #region 缩放
        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            if (e.Delta >= 0)
            {
                hWindowControl1.Width = (int)(hWindowControl1.Width * 1.1);//因为Widthh和Height都是int类型，所以要强制转换一下-_-||
                hWindowControl1.Height = (int)(hWindowControl1.Height * 1.1);
                HOperatorSet.DispObj(xx, hWindowControl1.HalconWindow);
            }
            else
            {
                hWindowControl1.Width = (int)(hWindowControl1.Width * 0.9);
                hWindowControl1.Height = (int)(hWindowControl1.Height * 0.9);
                HOperatorSet.DispObj(xx, hWindowControl1.HalconWindow);
            }
            #endregion
        }
    }
}